﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.PoseEditor.CUiKinPoseEditorCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using COMMANDPARAMSLib;
using DnProcessSimulateCommands.JointJog;
using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.CommandParameters;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.PoseEditor;

public sealed class CUiKinPoseEditorCmd : TxButtonCommand
{
  private TxObjectList m_deviceList = new TxObjectList();
  private CUiKinPoseEditorEnabler m_enabler = new CUiKinPoseEditorEnabler();

  public virtual string Category => PoseEditorStringTable.CMD_CATEGORY;

  public virtual string Name => PoseEditorStringTable.CMD_NAME;

  public virtual string Description => PoseEditorStringTable.CMD_DESCRIPTION;

  public virtual string Tooltip => PoseEditorStringTable.CMD_TOOLTIP;

  public virtual string Bitmap => "PoseEditor.PoseEditorIcon.bmp";

  public virtual string LargeBitmap => "PoseEditor.PoseEditorIcon_LARGE.png";

  public virtual ITxImage Image => (ITxImage) TxImageManager.GetImageByKey("PoseEditor");

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_enabler;

  public virtual bool Licensed => TxCommandLicenser.PoseEditorLicense;

  public virtual void Execute(object cmdParams)
  {
    ITxDevice device = (ITxDevice) null;
    TxPose pose = (TxPose) null;
    if (cmdParams != null)
    {
      TxSingleObjectParameters objectParameters = this.GetSingleObjectParameters(cmdParams);
      if (objectParameters != null)
      {
        device = objectParameters.Object as ITxDevice;
        pose = objectParameters.Object as TxPose;
      }
    }
    else
    {
      TxObjectList items = TxApplication.ActiveSelection.GetItems();
      if (((Collection<ITxObject>) items).Count == 1)
      {
        device = ((Collection<ITxObject>) items)[0] as ITxDevice;
        pose = ((Collection<ITxObject>) items)[0] as TxPose;
      }
    }
    if (device != null && !((Collection<ITxObject>) this.m_deviceList).Contains((ITxObject) device))
    {
      CUiKinPoseEditorForm kinPoseEditorForm = new CUiKinPoseEditorForm();
      ((Form) kinPoseEditorForm).FormClosed += new FormClosedEventHandler(this.form_FormClosed);
      kinPoseEditorForm.Show(device);
      ((Collection<ITxObject>) this.m_deviceList).Add((ITxObject) device);
    }
    else
    {
      if (!TxPose.op_Inequality(pose, (ITxObject) null))
        return;
      this.ActivateCommandOnPose(pose, device);
    }
  }

  private void ActivateCommandOnPose(TxPose pose, ITxDevice device)
  {
    CUiKinJointJogForm cuiKinJointJogForm = new CUiKinJointJogForm();
    cuiKinJointJogForm.PoseName = pose.Name;
    cuiKinJointJogForm.PoseNameReadOnly = !pose.CanBeRenamed;
    cuiKinJointJogForm.OkEnabled = pose.CanBeModified;
    cuiKinJointJogForm.Pose = pose;
    this.JumpToPose(pose);
    if (!cuiKinJointJogForm.ShowModeless(pose.Device, pose.PoseData, PoseEditorStringTable.EDIT_POSE_TITLE))
      return;
    ((Form) cuiKinJointJogForm).FormClosed += new FormClosedEventHandler(CUiKinPoseEditorCmd.ClosedEventHandler);
  }

  private static void ClosedEventHandler(object sender, FormClosedEventArgs es)
  {
    if (!(sender is CUiKinJointJogForm cuiKinJointJogForm) || cuiKinJointJogForm.CloseDialogResult != DialogResult.OK)
      return;
    TxPose pose = cuiKinJointJogForm.Pose;
    if (!TxPose.op_Inequality(pose, (ITxObject) null))
      return;
    using (TxPoseData currentPose = pose.Device.CurrentPose)
    {
      pose.PoseData = currentPose;
      if (cuiKinJointJogForm.PoseNameReadOnly)
        return;
      pose.Name = cuiKinJointJogForm.PoseName;
    }
  }

  private void JumpToPose(TxPose pose)
  {
    ITxDevice device = pose.Device;
    if (new TxDevicePoseData(device, device.CurrentPose).Equals((object) new TxDevicePoseData(device, pose.PoseData)))
      return;
    device.CurrentPose = pose.PoseData;
    TxApplication.RefreshDisplay();
  }

  private void form_FormClosed(object sender, FormClosedEventArgs e)
  {
    ((Collection<ITxObject>) this.m_deviceList).Remove((ITxObject) (sender as CUiKinPoseEditorForm).Device);
  }

  private TxSingleObjectParameters GetSingleObjectParameters(object cmdParams)
  {
    switch (cmdParams)
    {
      case ICoSingleObjectParams singleObjectParams:
        pattern_0 = new TxSingleObjectParameters();
        ITApiObj apiObj = (ITApiObj) null;
        singleObjectParams.GetObj(ref apiObj);
        if (apiObj != null)
        {
          ITxObject objectFromApiObj = this.GetObjectFromApiObj(apiObj);
          pattern_0.Object = objectFromApiObj;
          break;
        }
        break;
    }
    return pattern_0;
  }

  private ITxObject GetObjectFromApiObj(ITApiObj apiObj)
  {
    string str;
    apiObj.DumpToString(ref str);
    ITxObject objectFromApiObj = (ITxObject) null;
    if (str != null)
      objectFromApiObj = TxConvertor.tID2Object(str);
    return objectFromApiObj;
  }
}
